////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// shorter get

var $id = function(id){return document.getElementById(id);};
var $class = function(classname){return document.getElementsByClassName(classname);};
var $name = function(name){return document.getElementsByName(name);};
var $tag = function(tag){return document.getElementsByTagName(tag);};

// remove()
Element.prototype.remove=function(){this.parentElement.removeChild(this);}
NodeList.prototype.remove=HTMLCollection.prototype.remove=function(){for(var i=0,len=this.length;i<len;i++){
if(this[i] && this[i].parentElement){this[i].parentElement.removeChild(this[i]);}}}

t2d = function(unix){
	time = new Date(unix*1000);
	return time.getFullYear()+'.'+('0'+(time.getMonth()+1)).slice(-2)+'.'+('0'+time.getDate()).slice(-2)+' '+('0'+time.getHours()).slice(-2)+':'+('0'+time.getMinutes()).slice(-2)+':'+('0'+time.getSeconds()).slice(-2);
}

var2text = function(inputText){
	text = inputText.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/\n/g,'<br/>');
	return text;
}
doc2text=function(t){
	// проверка < и > в именах
	t2 = t.replace(/&/g,'&amp;').replace(/</g, '&lt;').replace(/>/g,'&gt;').replace(/"/g, '&quot;');
	return t2;
}
len2t = function(sec){
	if(Math.floor(sec/3600) > 0){
		return Math.floor(sec/3600)+':'+('0'+(Math.floor(sec/60))).slice(-2)+':'+('0'+(sec%60)).slice(-2);
	}
	else{
		return Math.floor(sec/60)+':'+('0'+(sec%60)).slice(-2);
	}
}

function getVal(val){
   if (isNaN(val)) return 0;
   else return val;
}

// error box
function openAlertBox(text,button1,button2){
	$tag('body')[0].style.overflow = 'hidden';
	$id('alertbg').style.visibility = 'visible';
	// text box
	$id('alerttext').innerHTML = text;
	// buttons
	div = document.createElement('div');
	if(button2==0){
		$id('alertbuttons').innerHTML = '<button onclick="'+button1+'">OK</button>';
	}
	else{
		$id('alertbuttons').innerHTML = ''
			+'<button onclick="'+button1+'">OK</button>'
			+'<button onclick="'+button2+'">Отмена</button>';
	}
	// margin
	$id('alertbox').style.marginLeft = '-' + (($id('alerttext').offsetWidth+2)/2) +'px';
	$id('alertbox').style.marginTop = '-' + ($id('alertbox').offsetHeight/2) +'px';
}
function closeAlertBox(){
	$id('alertbox').style.marginLeft = '';
	$id('alertbox').style.marginTop = '';
	$id('alerttext').innerHTML = '';
	$id('alertbuttons').innerHTML = '';
	$id('alertbg').style.visibility = 'hidden';
	$tag('body')[0].style.overflow = 'auto';
}

// show / hide
function toggle(el) {el.style.display = (el.style.display == 'none') ? '' : 'none'}

var apicall=function(e,t,n){var r,i="",s=document.createElement("script");for(var o in t){i+=encodeURIComponent(o)+"="+encodeURIComponent(t[o])+"&"}
r="__json"+Math.floor(Math.random()*1000001);window[r]=function(e){s.remove();delete window[r];n(e)};s.src="https://api.vk.com/method/"+e+"?"
+i+"v="+apicfg.ver+"&lang="+apicfg.lang+"&access_token="+apicfg.token+"&callback="+r;document.getElementsByTagName("head")[0].appendChild(s)}

JSON.stringify=JSON.stringify||function(e){var t=typeof e;if(t!="object"||e===null){if(t=="string")e='"'+e+'"';return String(e)}
else{var n,r,i=[],s=e&&e.constructor==Array;for(n in e){r=e[n];t=typeof r;if(t=="string")r='"'+r+'"';else if(t=="object"&&r!==null)r=JSON.stringify(r);
i.push((s?"":'"'+n+'":')+String(r))}return(s?"[":"{")+String(i)+(s?"]":"}")}}

getData=function(arr,elem,value){for(var i in arr){if(arr[i][elem]===value)return arr[i]}}
function getURLParameter(name) {return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);}

show_page = getURLParameter('show');

temp = {};

///////////////////////////////////////////////////////////////////////////

window.onload = function(){
	if(apicfg.token == ''){
		span = document.createElement('span');
		span.innerHTML = ' [<a href="'+apiauth.url+'" target="_blank">Get Access Token</a>]';
		$id('main_nav').appendChild(span);
	}
	else{
		preapi();
	}
}

preapi = function(){

	apicall('users.get',{fields:'photo_50,online,last_seen'},function(user){
		console.log('user:');
		console.log(user);
		if(user.response){
			vkuser = user.response[0];
			temp.onl = vkuser.online == 1 ? '<a href="#" onclick="SetOfflineAction();return false;">online</a>' : 
				'<a title="Last seen: '+t2d(vkuser.last_seen.time)+'" href="#" onclick="SetOnlineAction();return false;">offline</a>';
			$class('ava')[0].style.backgroundImage="url('"+vkuser.photo_50+"')";
			$class('ln1')[0].innerHTML=vkuser.first_name+' '+vkuser.last_name+' ['+temp.onl+']';
			
			apicall('account.getCounters',{filter:'friends,messages,photos,videos,notes,gifts,events,groups,notifications'},function(menu){
				console.log('user menu counters:');
				console.log(menu);
				if(menu.response){
					span = document.createElement('span');
					span.innerHTML = ' [<a href="?show=im">Диалоги'+(menu.response.messages?' ('+menu.response.messages+')':'')+'</a>]';
					$id('main_nav').appendChild(span);
					span = document.createElement('span');
					span.innerHTML = ' [<a href="?show=groups">Сообщества</a>]';
					$id('main_nav').appendChild(span);
					if(apiTestConsole){
						span = document.createElement('span');
						span.innerHTML = ' [<a href="#" onclick="openApiConsole();return false;">Вызов API</a>]';
						$id('main_nav').appendChild(span);
					}
				}
				else{
					console.log('error loading:');
					console.log(menu);
				}
			});
			
			// go api
			doapis();
		}
	});

}

function SetOnlineAction(){
	openAlertBox('Перейти в онлайн на 15 минут?','doSetOnline();closeAlertBox();','closeAlertBox();');
}
function SetOfflineAction(){
	openAlertBox('Перейти в оффлайн?','doSetOffline();closeAlertBox();','closeAlertBox();');
}

function doSetOnline(){
	apicall('account.setOnline',{},function(data){
		if(data.response){
			location.reload(); 
		}
		else{
			openAlertBox(''
				+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
			'closeAlertBox();',0);
		}
	})
}

function doSetOffline(){
	apicall('account.setOffline',{},function(data){
		if(data.response){
			location.reload(); 
		}
		else{
			openAlertBox(''
				+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
			'closeAlertBox();',0);
		}
	})
}

//////////////////////////////////////////////////////////////////////////
function doapis(){

// show profile info
show_profile = function(id){
	temp.fields='id,first_name,last_name,deactivated,verified,blacklisted,sex,bdate,city,country,home_town,photo_50,photo_100,photo_max_orig,online,'
		+'lists,domain,has_mobile,contacts,site,education,universities,schools,status,last_seen,followers_count,common_count,counters,nickname,'
		+'relatives,relation,personal,connections,exports,wall_comments,activities,interests,music,movies,tv,books,games,about,quotes,'+
		'can_post,can_see_all_posts,can_see_audio,can_write_private_message,timezone'
	apicall('users.get',{user_ids:id,fields:temp.fields},function(data){
		if(data.response){
			console.log('user data:')
			console.log(data.response[0]);
			temp.vk_url = 'http://vk.com/'+data.response[0].domain;
			temp.sex_text = data.response[0].sex == 0 ? '?' : data.response[0].sex == 1 ? 'Женский' : 'Мужской';
			openAlertBox(''
				+'<div style="overflow:hidden;"><img style="float:left;" src="'+data.response[0].photo_50+'"/>'
					+'<div style="text-align:left;margin-left:55px;">'
						+'<div><a href="'+temp.vk_url+'" target="_blank">'+data.response[0].first_name+' '+data.response[0].last_name+'</a> @ VK.com</div>'
						+'<div>Пол: '+temp.sex_text+'</div>'
					+'</div>'
				+'</div>'
				+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response[0])+'</textarea>',
			'closeAlertBox();',0);
		}
	});
}

if(show_page == "profile"){

}

// im
if(show_page == "im"){
	user_id = getURLParameter('uid') != 'null' && !isNaN(getURLParameter('uid')) ? getURLParameter('uid') : 0;
	chat_id = getURLParameter('cid') != 'null' && !isNaN(getURLParameter('cid')) ? getURLParameter('cid') : 0;
	
	if(user_id == 0 && chat_id == 0){
		temp.apiim='var m=API.messages.getDialogs({count:100,offset:0,preview_length:110});'
			+'return {mail:m,users:API.users.get({user_ids:m.items@.message@.user_id,fields:"photo_50,online"})};';
		apicall('execute',{code:temp['apiim']},function(data){
			//console.log('im data:');
			//console.log(data);
			if(data.response){
				div = document.createElement('div');
				div.className = 'tminibox';
				div.innerHTML = '<a href="">Сообщения (IM)</a>';
				main = $id('main');
				main.appendChild(div);
				for(msg=0;msg<data.response.mail.items.length;msg++){
					//console.log('msg data:');
					//console.log(data.response.mail.items[msg]);
					div = document.createElement('div');
					div.className = 'title';
					div.style.textAlign = 'left';
					if(data.response.mail.items[msg].message.chat_id != null){
						div.innerHTML = '[#'+data.response.mail.items[msg].message.id+'] '
							+'<a href="?show=im&amp;cid='+data.response.mail.items[msg].message.chat_id+'">Беседа: '+var2text(data.response.mail.items[msg].message.title)+'</a> '
							+'@ '+t2d(data.response.mail.items[msg].message.date);
						main = $id('main');
						main.appendChild(div);
						// text + console
						temp.chatava = data.response.mail.items[msg].message.photo_50 ? data.response.mail.items[msg].message.photo_50 : 'http://vk.com/images/camera_c.gif';
						div = document.createElement('div');
						div.className = 'add_up';
						div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
							+'<img src="'+temp.chatava+'" style="float:left;" alt="">'
								+'<div style="margin-left:55px;">'
									+'<img style="width:25px;height:25px;float:left;" src="'+getData(data.response.users,'id',data.response.mail.items[msg].message.user_id).photo_50+'" alt=""/>'
									+'<div style="margin-left:30px;">'+var2text(data.response.mail.items[msg].message.body)+'</div>'
								+'</div>'
							+'</div>'
							+'<hr/><div>[<a href="?show=im&amp;cid='+data.response.mail.items[msg].message.chat_id+'">открыть беседу'+(data.response.mail.items[msg].unread?' ('+data.response.mail.items[msg].unread+')':'')+'</a>]</div>'
							+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response.mail.items[msg].message)+'</textarea>';
						main = $id('main');
						main.appendChild(div);
					}
					else{
						temp.inout = data.response.mail.items[msg].message.out == 1 ? 'outbox' : 'inbox';
						temp.onl = getData(data.response.users,'id',data.response.mail.items[msg].message.user_id).online == 1 ? 'online' : 'offline';
						div.innerHTML = '[#'+data.response.mail.items[msg].message.id+'] '
							+'<a href="#" onclick="show_profile(\''+data.response.mail.items[msg].message.user_id+'\');return false;">'
								+getData(data.response.users,'id',data.response.mail.items[msg].message.user_id).first_name+' '
								+getData(data.response.users,'id',data.response.mail.items[msg].message.user_id).last_name
							+'</a> '
							+'['+temp.onl+'] '
							+'@ '+t2d(data.response.mail.items[msg].message.date);
						main = $id('main');
						main.appendChild(div);
						// text + console
						div = document.createElement('div');
						div.className = 'add_up';
						if(temp.inout == 'inbox'){
							div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
									+'<img src="'+getData(data.response.users,'id',data.response.mail.items[msg].message.user_id).photo_50+'" style="float:left;" alt="">'
									+'<div style="margin-left:55px;">'
										+'<div>'+var2text(data.response.mail.items[msg].message.body)+'</div>'
									+'</div>'
								+'</div>'
								+'<hr/><div>[<a href="?show=im&amp;uid='+data.response.mail.items[msg].message.user_id+'">открыть диалог'+(data.response.mail.items[msg].unread?' ('+data.response.mail.items[msg].unread+')':'')+'</a>]</div>'
								+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response.mail.items[msg].message)+'</textarea>';
						}
						else if(temp.inout == 'outbox'){
							div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
									+'<img src="'+getData(data.response.users,'id',data.response.mail.items[msg].message.user_id).photo_50+'" style="float:left;" alt="">'
									+'<div style="margin-left:55px;">'
										+'<div>'
											+'<img src="'+vkuser.photo_50+'" style="width:25px;height:25px;float:left;">'
											+'<div style="margin-left:30px;">'
												+var2text(data.response.mail.items[msg].message.body)
											+'</div>'
										+'</div>'
									+'</div>'
								+'</div>'
								+'<hr/><div>[<a href="?show=im&amp;uid='+data.response.mail.items[msg].message.user_id+'">открыть диалог</a>]</div>'
								+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response.mail.items[msg].message)+'</textarea>';
						}
						main = $id('main');
						main.appendChild(div);
					}
				}
			}
		})
	}
	else if(chat_id == 0 && user_id > 0){
		// replay
		temp.apichat = 'var m=API.messages.getHistory({"user_id":'+user_id+',offset:0,count:100});'
			+'var u=API.users.get({user_ids:'+user_id+',fields:"photo_50,can_write_private_message,online,last_seen"});'
			+'return {to:'+user_id+',mail:m,users:u};';
		apicall('execute',{code:temp['apichat']},function(data){
			console.log('chat data:');
			console.log(data);
			if(data.response){
				temp.onl = data.response.users[0].online == 1 ? 'online' : 
					'Last seen: '+t2d(data.response.users[0].last_seen.time);
				div = document.createElement('div');
				div.className = 'tminibox';
				div.innerHTML = '<div>Просмотр диалога:</div>'
					+'<div>'
					+'<a href="#" onclick="show_profile(\''+data.response.users[0].id+'\');return false;">'
						+data.response.users[0].first_name+' '
						+data.response.users[0].last_name
					+'</a> '
					+'['+temp.onl+']'
					+'</div>'
				main = $id('main');
				main.appendChild(div);
				temp.replyb = data.response.users[0].can_write_private_message == 1 ?
					'[<a href="#" onclick="toggle(replayboxheader);toggle(replayboxbody);return false;">show/hide replay box</a>]' : '';
				div = document.createElement('div');
				div.className = 'minibox';
				div.innerHTML = '[<a href="">refresh</a>] '
					+temp.replyb;
				main = $id('main');
				main.appendChild(div);
				if(data.response.users[0].can_write_private_message==1){
					div = document.createElement('div');
					div.id = 'replayboxheader';
					div.className = 'title';
					div.style.display = 'none'; 
					div.innerHTML = 'replay:';
					main = $id('main');
					main.appendChild(div);
					div = document.createElement('div');
					div.id = 'replayboxbody';
					div.className = 'add_up';
					div.style.display = 'none'; 
					div.innerHTML = '<form id="send_msg">'
						+'<div>message:</div><textarea type="text" name="msg_txt"></textarea>'
						+'<div>attachment:</div><textarea style="height:20px;" type="text" name="msg_att"></textarea>'
						+'<div>forward_messages:</div><textarea style="height:20px;" type="text" name="msg_frd"></textarea>'
						+'<button onclick="postreplay(\''+user_id+'\',\'\',$name(\'msg_txt\')[0].value,$name(\'msg_att\')[0].value,$name(\'msg_frd\')[0].value);return false;">Send</button>'
					+'</form>';
					main = $id('main');
					main.appendChild(div);
				}
				for(msg=0;msg<data.response.mail.items.length;msg++){
					div = document.createElement('div');
					div.className = 'title';
					div.style.textAlign = 'left';
					
					temp.onl0 = vkuser.online == 1 ? 'online' : 'offline';
					temp.onl1 = data.response.users[0].online == 1 ? 'online' : 'offline';
					
					div.innerHTML = '[#'+data.response.mail.items[msg].id+'] <a href="#" onclick="show_profile(\''+data.response.mail.items[msg].from_id+'\');return false;">'
							+(data.response.mail.items[msg].from_id==vkuser.id?vkuser.first_name:data.response.users[0].first_name)+' '
							+(data.response.mail.items[msg].from_id==vkuser.id?vkuser.last_name:data.response.users[0].last_name)
						+'</a> '
						+'['+(data.response.mail.items[msg].from_id==vkuser.id?temp.onl0:temp.onl1)+'] '
						+'@ '+t2d(data.response.mail.items[msg].date)+' '
						+(data.response.mail.items[msg].read_state==0? (data.response.mail.items[msg].from_id!=vkuser.id?'[<a href="#" onclick="setMsgAsRead('+data.response.mail.items[msg].from_id+','+data.response.mail.items[msg].id+')">new</a>]':'[new]') :'');
					main = $id('main');
					main.appendChild(div);
					
					temp.atts='';
					if(data.response.mail.items[msg].attachments !== undefined){
						temp.atts='<div class="attacments"> <b>Материалы:</b> </div>';
						l=data.response.mail.items[msg].attachments.length;
						for(k=0;k<l;k++){
							temp.atts=temp.atts+make_attach(data.response.mail.items[msg].attachments[k]);
						}
					}
					temp.fwd = '';
					if(data.response.mail.items[msg].fwd_messages !== undefined){
						temp.fwd='<hr/><div class="attacments"> <b>Пересланные сообщения:</b> </div><hr/>';
						for(msgfwd=0;msgfwd<data.response.mail.items[msg].fwd_messages.length;msgfwd++){
							temp.fwdatts='';
							if(data.response.mail.items[msg].fwd_messages[msgfwd].attachments !== undefined){
								temp.fwdatts='<div class="attacments"> <b>Материалы:</b> </div>';
								fwdl=data.response.mail.items[msg].fwd_messages[msgfwd].attachments.length;
								for(fwdk=0;fwdk<fwdl;fwdk++){
									temp.fwdatts=temp.fwdatts+make_attach(data.response.mail.items[msg].fwd_messages[msgfwd].attachments[fwdk]);
								}
							}
							temp.attmsg = data.response.mail.items[msg].fwd_messages[msgfwd].fwd_messages !== undefined ? '<div>[att msgs]</div>' : '';
							temp.fwd=temp.fwd
								+'<div>'
									+data.response.mail.items[msg].fwd_messages[msgfwd].user_id+' @ '+t2d(data.response.mail.items[msg].fwd_messages[msgfwd].date)
								+'</div>'
								+'<div>'
									+data.response.mail.items[msg].fwd_messages[msgfwd].body
									+temp.fwdatts
									+temp.attmsg
								+'</div>';
						}
					}
					
					div = document.createElement('div');
					div.className = 'add_up';
					div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
						+'<img src="'+(data.response.mail.items[msg].from_id==vkuser.id?vkuser.photo_50:data.response.users[0].photo_50)+'" style="float:left;" alt="">'
							+'<div style="margin-left:55px;">'
								+'<div>'
									+var2text(data.response.mail.items[msg].body)
								+'</div>'
								+'<div>'+temp.atts+'</div>'
								+temp.fwd
							+'</div>'
						+'</div>'
						+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response.mail.items[msg])+'</textarea>';
					main = $id('main');
					main.appendChild(div);
				}
			}
		});
	}
	else if(user_id == 0 && chat_id > 0){
		temp.apichat='var m=API.messages.getHistory({"chat_id":'+chat_id+',offset:0,count:100});'
			+'var u=API.messages.getChat({chat_id:'+chat_id+',fields:"photo_50,online"});'
			+'return {to:-'+chat_id+',mail:m,chat:u};';
		apicall('execute',{code:temp['apichat']},function(data){
			console.log('chat data:');
			console.log(data);
			if(data.response){
				div = document.createElement('div');
				div.className = 'tminibox';
				div.innerHTML = var2text(data.response.chat.title)+' (chat #'+data.response.chat.id+')';
				main = $id('main');
				main.appendChild(div);
				div = document.createElement('div');
				div.className = 'minibox';
				div.innerHTML = '[<a href="">refresh</a>] '
					+'[<a href="#" onclick="toggle(replayboxheader);toggle(replayboxbody);return false;">show/hide replay box</a>]';
				main = $id('main');
				main.appendChild(div);
				div = document.createElement('div');
				div.id = 'replayboxheader';
				div.className = 'title';
				div.style.display = 'none'; 
				div.innerHTML = 'replay:'
				main = $id('main');
				main.appendChild(div);
				div = document.createElement('div');
				div.id = 'replayboxbody';
				div.className = 'add_up';
				div.style.display = 'none'; 
				div.innerHTML = '<form id="send_msg">'
					+'<div>message:</div><textarea type="text" name="msg_txt"></textarea>'
					+'<div>attachment:</div><textarea style="height:20px;" type="text" name="msg_att"></textarea>'
					+'<div>forward_messages:</div><textarea style="height:20px;" type="text" name="msg_frd"></textarea>'
					+'<button onclick="postreplay(\'\',\''+chat_id+'\',$name(\'msg_txt\')[0].value,$name(\'msg_att\')[0].value,$name(\'msg_frd\')[0].value);return false;">Send</button>'
				+'</form>';
				main = $id('main');
				main.appendChild(div);
				for(msg=0;msg<data.response.mail.items.length;msg++){
					div = document.createElement('div');
					div.className = 'title';
					div.style.textAlign = 'left';
					div.innerHTML = '[#'+data.response.mail.items[msg].id+'] <a href="#" onclick="show_profile(\''+data.response.mail.items[msg].from_id+'\');return false;">'
						+getData(data.response.chat.users,'id',data.response.mail.items[msg].from_id).first_name+' '
						+getData(data.response.chat.users,'id',data.response.mail.items[msg].from_id).last_name+'</a>'
						+' @ '+t2d(data.response.mail.items[msg].date)+' '
						+(data.response.mail.items[msg].read_state==0?
							(data.response.mail.items[msg].from_id!=vkuser.id?
								'[<a href="#" onclick="setMsgAsRead('+data.response.to+','+data.response.mail.items[msg].id+')">new</a>]':
								'[new]')
							:'');
					main = $id('main');
					main.appendChild(div);
					div = document.createElement('div');
					div.className = 'add_up';
					div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
						+'<img src="'+getData(data.response.chat.users,'id',data.response.mail.items[msg].from_id).photo_50+'" style="float:left;" alt="">'
							+'<div style="margin-left:55px;">'
								+'<div>'
									+var2text(data.response.mail.items[msg].body)
								+'</div>'
							+'</div>'
						+'</div>'
						+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response.mail.items[msg])+'</textarea>';
					main = $id('main');
					main.appendChild(div);
				}
			}
		});
	}
	postreplay = function(user_id,chat_id,message,attachment,forward_messages){
		json_req = {message:message,attachment:attachment,forward_messages:forward_messages};
		temp.param_to = chat_id == '' ? 'user_id' : 'chat_id';
		temp.param_id = chat_id == '' ? user_id : chat_id;
		json_req[temp.param_to] = temp.param_id;
		apicall('messages.send',json_req,function(data){
			if(data.response){
				location.reload(); 
			}
			else{
				openAlertBox(''
					+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
				'closeAlertBox();',0);
			}
		});
	}
}

setMsgAsRead = function(user_id,msg){
	openAlertBox('<div>Пометить сообщения #'+msg+' и старше в этом '+(user_id>0?'диалоге':'чате')+' как прочитанные?</div>','markMsgAsRead('+user_id+','+msg+');closeAlertBox();','closeAlertBox();');
	markMsgAsRead = function(user_id,msg){
		apicall('messages.markAsRead',{message_ids:msg,user_id:user_id},function(data){
			if(data.response==1){
				location.reload();
			}
			else{
				openAlertBox(''
					+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
				'closeAlertBox();',0);
			}
		});
	}
}

if(show_page == "groups"){
	apicall('groups.get',{extended:1,fields:'members_count'},function(data){
		if(data.response){
			console.log(data.response);
			for(i=0;i<data.response.items.length;i++){
				// title
				temp.grt = data.response.items[i].type == 'group' ? 'Группа' : 
					data.response.items[i].type == 'page' ? 'Публичная страница' : '[ Неизв. ('+data.response.items[i].type+') ]';
				temp.adm = data.response.items[i].is_admin == 0 ? 'Member' :
					data.response.items[i].admin_level == 3 ? 'Admin' :
					data.response.items[i].admin_level == 2 ? 'Editor' :
					data.response.items[i].admin_level == 1 ? 'Moderator' : 'Member';
				div = document.createElement('div');
				div.className = 'title';
				div.innerHTML = '<a href="?show=group&amp;id='+data.response.items[i].id+'">'+var2text(data.response.items[i].name)+'</a>'
				main = $id('main');
				main.appendChild(div);
				// text+console
				div = document.createElement('div');
				div.className = 'add_up';
				div.innerHTML = '<div style="overflow:hidden;">'
						+'<img src="'+data.response.items[i].photo_100+'" alt="[icon]" style="float:left;width:48px;height:48px;"/>'
						+'<div style="margin-left:50px;">'
							+'<div>'+temp.grt+'</div>'
							+'<div>Участников: '+data.response.items[i].members_count+' </div>'
							+'<div>'+temp.adm+'</div>'
						+'</div>'
					+'</div>'
					+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response.items[i])+'</textarea>';
				main = $id('main');
				main.appendChild(div);
			}
		}
	});
}

if(show_page == "group"){
	// group + group wall
	group_id = getURLParameter('id') != 'null' && !isNaN(getURLParameter('id')) ? getURLParameter('id') : 0;
	page_id = getURLParameter('page') != 'null' && !isNaN(getURLParameter('page')) ? getURLParameter('page') : 1;
	if(group_id < 1){
		div = document.createElement('div');
		div.className = 'title';
		div.innerHTML = 'Ошибка:'
		main = $id('main');
		main.appendChild(div);
		div = document.createElement('div');
		div.className = 'add_up';
		div.innerHTML = 'Группа не выбрана';
		main = $id('main');
		main.appendChild(div);
	}
	else{
		apicall('groups.getById',{group_id:group_id,fields:'members_count,wiki_page,counters,can_see_all_post,can_post'},function(data){
			// temp
			temp.grt = data.response[0].type == 'group' ? 'Группа' : 
				data.response[0].type == 'page' ? 'Публичная страница' : '[ Неизв. ('+data.response.items[i].type+') ]';
			temp.adm = data.response[0].is_admin == 0 ? 'Участник' :
				data.response[0].admin_level == 3 ? 'Admin' :
				data.response[0].admin_level == 2 ? 'Editor' :
				data.response[0].admin_level == 1 ? 'Moderator' : 'Участник';
			
			div = document.createElement('div');
			div.className = 'title';
			div.innerHTML = '<a href="?show=group&amp;id='+data.response[0].id+'">'+var2text(data.response[0].name)+'</a>'
			main = $id('main');
			main.appendChild(div);
			div = document.createElement('div');
			div.className = 'add_up';
			div.innerHTML = '<div style="overflow:hidden;">'
						+'<img src="'+data.response[0].photo_100+'" alt="[icon]" style="float:left;width:48px;height:48px;"/>'
						+'<div style="margin-left:50px;">'
							+'<div>'+temp.grt+'</div>'
							+'<div>Участников: '+data.response[0].members_count+' </div>'
							+'<div>'+temp.adm+'</div>'
						+'</div>'
					+'</div>'
					+'<hr/>'
					+'<div>'
						+'<div>Обсуждений: '+getVal(data.response[0].counters.topics)+'</div>'
						+'<div>Видеозаписей: '+getVal(data.response[0].counters.videos)+'</div>'
						+'<div>Аудиозаписей: '+getVal(data.response[0].counters.audios)+'</div>'
						+'<div>Альбомов: '+getVal(data.response[0].counters.albums)+'</div>'
						+'<div>Фото: '+getVal(data.response[0].counters.photos)+'</div>'
						+'<div>Документов: '+getVal(data.response[0].counters.docs)+'</div>'
					+'</div>'
					+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(data.response[0])+'</textarea>';
			main = $id('main');
			main.appendChild(div);
			// replay
			div = document.createElement('div');
			div.className = 'title';
			div.innerHTML = 'replay:'
			main = $id('main');
			main.appendChild(div);
			div = document.createElement('div');
			div.className = 'add_up';
			div.innerHTML = '<form id="send_msg">'
				+'<div>message:</div><textarea type="text" name="msg_txt"></textarea>'
				+'<div>attachment:</div><textarea style="height:20px;" type="text" name="msg_att"></textarea>'
				+'<div>'
					+'from group: <input style="display:inline-block;" type="checkbox" name="post_fg"/> '
					+'signed: <input style="display:inline-block;" type="checkbox" name="post_sg"/> '
				+'</div>'
				+'<button onclick="postwall('+group_id+',Number($name(\'post_fg\')[0].checked),Number($name(\'post_sg\')[0].checked),$name(\'msg_txt\')[0].value,$name(\'msg_att\')[0].value);return false;">Send</button>'
			+'</form>';
			main = $id('main');
			main.appendChild(div);
			temp.walloffset = (page_id-1)*100;
			apicall('wall.get',{owner_id:-group_id,count:100,offset:temp.walloffset,extended:1},function(wdata){
				console.log('wall data:');
				console.log(wdata);
				//console.log(wdata.response.profiles.id);
				if(wdata.response){
					for(i=0;i<wdata.response.items.length;i++){
						div = document.createElement('div');
						div.className = 'title';
						div.style.textAlign = 'left';
						temp.olk = wdata.response.items[i].from_id > 0 ? '<a href="#" onclick="show_profile(\''+wdata.response.items[i].from_id+'\');return false;">'
								+getData(wdata.response.profiles,'id',wdata.response.items[i].from_id).first_name+' '+getData(wdata.response.profiles,'id',wdata.response.items[i].from_id).last_name+'</a>':
							wdata.response.items[i].signer_id ? '<a href="#" onclick="show_profile(\''+wdata.response.items[i].signer_id+'\');return false;">admin</a> (signed)' :
							wdata.response.items[i].created_by ? '<a href="#" onclick="show_profile(\''+wdata.response.items[i].created_by+'\');return false;">admin</a>' : 'admin'
						div.innerHTML = '[#'+wdata.response.items[i].id+'] '+temp.olk+' @ '+t2d(wdata.response.items[i].date);
						main = $id('main');
						main.appendChild(div);
						// text+console
						temp.atts = '';
						temp.post_ava = wdata.response.items[i].from_id < 0 ? getData(wdata.response.groups,'id',-wdata.response.items[i].from_id).photo_50 :
							getData(wdata.response.profiles,'id',wdata.response.items[i].from_id).photo_50
						if(wdata.response.items[i].attachments != null){
							for(j=0;j<wdata.response.items[i].attachments.length;j++){
								temp.atts=temp.atts+make_attach(wdata.response.items[i].attachments[j]);
							}
						}
						temp.copy_wall = '';
						temp.copy_wall_atts = '';
						if(wdata.response.items[i].copy_history != null){
							console.log('copy')
							console.log(wdata.response.items[i].copy_history)
							for(w=0;w<wdata.response.items[i].copy_history.length;w++){
								if(wdata.response.items[i].copy_history[w].attachments != null){
									for(wa=0;wa<wdata.response.items[i].copy_history[w].attachments.length;wa++){
										temp.copy_wall_atts=temp.copy_wall_atts+make_attach(wdata.response.items[i].copy_history[w].attachments[wa]);
									}
								}
								temp.copy_wall=temp.copy_wall+'<div class="repost_text">'+wdata.response.items[i].copy_history[w].text+'</div>'
									+'<div class="repost_atts">'+temp.copy_wall_atts+'</div>';
							}
						}
						temp.delbtn = wdata.response.items[i].can_delete == 1 ? ' <span>[<a href="#" onclick="postdel('+wdata.response.items[i].owner_id+','+wdata.response.items[i].id+');return false;">Удалить</a>]</span>' : '';
						temp.like_count = wdata.response.items[i].likes.count;
						temp.like_action = wdata.response.items[i].likes.user_likes==0?'setLike(\'post\',\''+wdata.response.items[i].owner_id+'\',\''+wdata.response.items[i].id+'\',\'\');':'';
						temp.like_button = ' <span>[<a href="#" onclick="'+temp.like_action+'return false;">'+(wdata.response.items[i].likes.user_likes==0?'Like?':'Liked')+' ('+temp.like_count+')</a>]</span>';
						div = document.createElement('div');
						div.className = 'add_up';
						div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
								+'<img src="'+temp.post_ava+'" style="float:left;" alt="[ava]"/>'
								+'<div style="margin-left:55px;">'
									+'<div>'
										+var2text(wdata.response.items[i].text)
									+'</div>'
									+'<div style="overflow:hidden;">'+temp.atts+'</div>'
									+'<div style="overflow:hidden;border-width:0 0 0 1px;border-style:solid;border-color:black;padding-left:5px;">'+temp.copy_wall+'</div>'
								+'</div>'
							+'</div>'
							+'<hr/><div>'
								+'<div style="float:right;">'+temp.delbtn+temp.like_button+'</div>'
								+'[<a href="?show=wallpost&amp;oid='+wdata.response.items[i].owner_id+'&amp;pid='+wdata.response.items[i].id+'">читать комментарии ('+wdata.response.items[i].comments.count+')</a>]'
							+'</div>'
							+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(wdata.response.items[i])+'</textarea>';
						main = $id('main');
						main.appendChild(div);
					}
				}
			});
		});
		
		postwall = function(owner_id,from_group,signed,message,attachments){
			apicall('wall.post',{owner_id:-owner_id,from_group:from_group,signed:signed,message:message,attachments:attachments},function(data){
				if(data.response){
					location.reload(); 
				}
				else{
					openAlertBox(''
						+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
					'closeAlertBox();',0);
				}
			});
		}
	}
}

if(show_page == "wallpost"){
	owner_id = getURLParameter('oid') != 'null' && !isNaN(getURLParameter('oid')) ? getURLParameter('oid') : '';
	post_id = getURLParameter('pid') != 'null' && !isNaN(getURLParameter('pid')) ? getURLParameter('pid') : 0;
	page_id = getURLParameter('page') != 'null' && !isNaN(getURLParameter('page')) ? getURLParameter('page') : 1;
	
	// back to wall
	if(post_id!=0){
		apicall('wall.getById',{posts:owner_id+'_'+post_id,extended:1},function(pdata){
			console.log('Post:');
			console.log(pdata);
			console.log(pdata.items);
			//
			temp.walltitle = owner_id < 1 ? '<a href="?show=group&id='+(-owner_id)+'">'+doc2text(getData(pdata.response.groups,'id',-owner_id).name)+'</a>' :
				'userwallpost';
			div = document.createElement('div');
			div.className = 'tminibox';
			div.innerHTML = '@ '+temp.walltitle+'';
			main = $id('main');
			main.appendChild(div);
			//	
			if(pdata.response && pdata.response.items.length > 0){
				div = document.createElement('div');
				div.className = 'title';
				div.style.textAlign = 'left';
				temp.olk = pdata.response.items[0].from_id > 0 ? '<a href="#" onclick="show_profile(\''+pdata.response.items[0].from_id+'\');return false;">'
								+getData(pdata.response.profiles,'id',pdata.response.items[0].from_id).first_name+' '+getData(pdata.response.profiles,'id',pdata.response.items[0].from_id).last_name+'</a>':
							pdata.response.items[0].signer_id ? '<a href="#" onclick="show_profile(\''+pdata.response.items[0].signer_id+'\');return false;">admin</a> (signed)' :
							pdata.response.items[0].created_by ? '<a href="#" onclick="show_profile(\''+pdata.response.items[0].created_by+'\');return false;">admin</a>' : 'admin';
				div.innerHTML = '[#'+pdata.response.items[0].id+'] '+temp.olk+' @ '+t2d(pdata.response.items[0].date);
				main = $id('main');
				main.appendChild(div);
				temp.atts = '';
				temp.post_ava = pdata.response.items[0].from_id < 0 ? getData(pdata.response.groups,'id',-pdata.response.items[0].from_id).photo_50 :
					getData(pdata.response.profiles,'id',pdata.response.items[0].from_id).photo_50
				if(pdata.response.items[0].attachments != null){
					for(j=0;j<pdata.response.items[0].attachments.length;j++){
						temp.atts=temp.atts+make_attach(pdata.response.items[0].attachments[j]);
					}
				}
				temp.delbtn = pdata.response.items[0].can_delete == 1 ? ' <span>[<a href="#" onclick="postdel('+pdata.response.items[0].owner_id+','+pdata.response.items[0].id+');return false;">Удалить</a>]</span>' : '';
						temp.like_count = pdata.response.items[0].likes.count;
						temp.like_action = pdata.response.items[0].likes.user_likes==0?'setLike(\'post\',\''+pdata.response.items[0].owner_id+'\',\''+pdata.response.items[0].id+'\',\'\');':'';
						temp.like_button = ' <span>[<a href="#" onclick="'+temp.like_action+'return false;">'+(pdata.response.items[0].likes.user_likes==0?'Like?':'Liked')+' ('+temp.like_count+')</a>]</span>';
				div = document.createElement('div');
				div.className = 'add_up';
				div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
						+'<img src="'+temp.post_ava+'" style="float:left;" alt="[ava]"/>'
						+'<div style="margin-left:55px;">'
							+'<div>'
								+var2text(pdata.response.items[0].text)
							+'</div>'
							+'<div style="overflow:hidden;">'+temp.atts+'</div>'
							+'</div>'
					+'</div>'
					+'<hr/><div style="overflow:hidden">'
						+'<div style="float:right;">'+temp.delbtn+temp.like_button+'</div>'
					+'</div>'
					+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(pdata.response.items[0])+'</textarea>';
				main = $id('main');
				main.appendChild(div);
				// if found:
				temp.commoffset = (page_id-1)*100;
				temp.group_info = owner_id < 0 ? 'var g=API.groups.getById({group_ids:'+(-owner_id)+'});\n' : ''
				temp.apicomments = 'var items=API.wall.getComments({"owner_id":"'+owner_id+'","post_id":"'+post_id+'","count":"100","preview_length":"0","need_likes":1,"offset":"'+temp.commoffset+'"});\n'
					+'var u=API.users.get({user_ids:items.items@.from_id,fields:"photo_50,domain"});\n'
					+temp.group_info
					+'return {comments:items,users:u,group:g};';
				
				//apicall('wall.getComments',{owner_id:owner_id,post_id:post_id,count:100,preview_length:0,extended:1,need_likes:1,offset:temp.commoffset},function(cdata){
				apicall('execute',{code:temp.apicomments},function(cdata){
					console.log('comm data')
					console.log(cdata)
					if(cdata.response){
						div = document.createElement('div');
						div.className = 'tminibox';
						div.innerHTML = 'comments:';
						main = $id('main');
						main.appendChild(div);
						for(k=0;k<cdata.response.comments.items.length;k++){
							div = document.createElement('div');
							div.className = 'title';
							div.style.textAlign = 'left';
							temp.olk = cdata.response.comments.items[k].from_id > 0 ? 
								'<a href="#" onclick="show_profile(\''+cdata.response.comments.items[k].from_id+'\');return false;">'+getData(cdata.response.users,'id',cdata.response.comments.items[k].from_id).first_name+' '+getData(cdata.response.users,'id',cdata.response.comments.items[k].from_id).last_name+'</a>' :
								'admin'
							div.innerHTML = '[#'+cdata.response.comments.items[k].id+'] '+temp.olk+' @ '+t2d(cdata.response.comments.items[k].date);
							main = $id('main');
							main.appendChild(div);
							temp.post_ava = cdata.response.comments.items[k].from_id < 0 ? cdata.response.group[0].photo_50 :
								getData(cdata.response.users,'id',cdata.response.comments.items[k].from_id).photo_50
							//temp.post_ava = 'http://vk.com/images/camera_c.gif';
							temp.com_atts = '';
							if(cdata.response.comments.items[k].attachments != null){
								for(m=0;m<cdata.response.comments.items[k].attachments.length;m++){
									temp.com_atts=temp.com_atts+make_attach(cdata.response.comments.items[k].attachments[m]);
								}
							}
							temp.like_count = cdata.response.comments.items[k].likes.count;
							temp.like_action = cdata.response.comments.items[k].likes.user_likes==0?'setLike(\'comment\',\''+owner_id+'\',\''+cdata.response.comments.items[k].id+'\',\'\');':'';
							temp.like_button = ' <span>[<a href="#" onclick="'+temp.like_action+'return false;">'+(cdata.response.comments.items[k].likes.user_likes==0?'Like?':'Liked')+' ('+temp.like_count+')</a>]</span>';
							div = document.createElement('div');
							div.className = 'add_up';
							div.innerHTML = '<div class="msg_content" style="overflow:hidden;">'
									+'<img src="'+temp.post_ava+'" style="float:left;" alt="[ava]"/>'
									+'<div style="margin-left:55px;">'
										+'<div>'
											+var2text(cdata.response.comments.items[k].text)
										+'</div>'
										+'<div style="overflow:hidden;">'+temp.com_atts+'</div>'
										+'</div>'
									+'</div>'
								+'<hr/><div style="overflow:hidden">'
									+'<div style="float:right;">'+temp.like_button+'</div>'
								+'</div>'
								+'<hr/><textarea type="text" name="vdescription">'+JSON.stringify(cdata.response.comments.items[k])+'</textarea>';
							main = $id('main');
							main = $id('main');
							main.appendChild(div);
						}
					}
				});
			}
		})
	}
}

postdel = function(owner_id,post_id){
	apicall('wall.delete',{owner_id:owner_id,post_id:post_id},function(data){
		if(data.response){
			location.reload(); 
		}
		else{
			openAlertBox(''
				+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
			'closeAlertBox();',0);
		}
	});
}

setLike = function(type,owner_id,item_id,access_key){
	apicall('likes.add',{type:type,owner_id:owner_id,item_id:item_id,access_key:access_key},function(data){
		if(data.response){
			location.reload(); 
		}
		else{
			openAlertBox(''
					+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
				'closeAlertBox();',0);
		}
	});
}

function make_attach(attach){
	html='';
	if (!attach[attach.type]){ 
		console.log('Attach broken?',attach);
		attach[attach.type]={};
	}
	if(attach.type=="photo"){
		temp.photo_link = attach.photo.photo_2560 || attach.photo.photo_1280 || attach.photo.photo_807 || attach.photo.photo_604;
		temp.photo_size = attach.photo.photo_2560 ? 'w' : attach.photo.photo_1280 ? 'z' : attach.photo.photo_807 ? 'y' : 'x'
		temp.att_photo_style = 'background-color:#F1F1F1;display:block;height:87px;width:130px;overflow:hidden;text-align:center;margin:2.5px;';
		html='<div class="attacment attacment_photo" style="display:inline-flex;">'
				+'<a target="_blank" href="'+temp.photo_link+'?photo'+attach.photo.owner_id+'_'+attach.photo.id+temp.photo_size+'.jpg" style="'+temp.att_photo_style+'">'
					+'<img src="'+attach.photo.photo_130+'" alt="[photo]" style="display:inline-block;"/>'
				+'</a>'
			+'</div>';
	}
	else if(attach.type=="video"){
		html='<div class="attacment attacment_video"> <div class="att_ico att_video"></div> <a href="#?show=video&amp;oid='+attach.video["owner_id"]+'&amp;id='+attach.video["id"]+'" target="_blank">'+doc2text(attach.video["title"])+' ('+len2t(attach.video["duration"])+')</a></div>';
	}
	else if(attach.type=="audio"){
		html='<div class="attacment attacment_audio">'
				+'<div class="att_ico att_audio"></div>'
				+'<a target="_blank" href="'+attach.audio.url+'" title="[audio'+attach.audio.owner_id+'_'+attach.audio.id+']">'
					+doc2text(attach.audio["artist"])+' - '+doc2text(attach.audio["title"])+' ('+len2t(attach.audio["duration"])+')'
				+'</a>'
			+'</div>';
	}
	else if(attach.type=="doc"){
		html='<div class="attacment attacment_document"> <div class="att_ico att_doc"></div> <a target="_blank" href="'+attach.doc["url"]+'">'+doc2text(attach.doc["title"])+'</a></div>';
	}
	else if(attach.type=="wall"){
		html='<div class="attacment attacment_wallpost"> <div class="att_ico att_wall"></div> <a target="_blank" href="#wall'+attach.wall["to_id"]+'_'+attach.wall["id"]+'">[wall'+attach.wall["to_id"]+'_'+attach.wall["id"]+']</a></div>';
	}
	else if(attach.type=="link"){
		html='<div class="attacment attacment_link"> <div class="att_ico att_link"></div> <a href="'+attach.link["url"]+'" target="_blank"><span>Ссылка:</span> '+doc2text(attach.link["title"])+'</a></div>';
	}
	else if(attach.type=="gift"){
		html='<div class="attacment attacment_gift"> <div class="att_ico att_gift"></div> <a target="_blank" href="'+attach.gift["thumb_256"]+'">Gift #'+attach.gift["id"]+'</a></div>';
	}
	else{
		console.log('attach "'+attach.type+'" is unknown');
	}
	return html
}

apiWallPhotoUpload=function(group_id){
	apicall('photos.getWallUploadServer',{group_id:group_id},function(data){
		if(data.response){
			openAlertBox(''
					+'<form method="post" enctype="multipart/form-data" action="'+data.response.upload_url+'">'
						+'<input type="file" name="file">'
						+'<input type="submit" value="Отправить">'
					+'</form>'
					+'<div><b>Save Command:</b></div>'
					+'<form id="do_vk_api">'
						+'<div>Method:</div>'
						+'<input type="text" name="apiconsolemethod" value="photos.saveWallPhoto">'
						+'<div>Parameters (in JSON):</div>'
						+'<input type="text" name="apiconsoleparams" value=\'{"user_id":"","group_id":"","server":"","hash":"","photo":""}\'>'
						+'<button onclick="prepApiCall();return false;">Do api request</button>'
					+'</form>',
				'closeAlertBox();',0);
		}
	});
	prepApiCall = function(){
		apimethod = $name('apiconsolemethod')[0].value;
		apiparams = JSON.parse($name('apiconsoleparams')[0].value);
		doApi(apimethod,apiparams);
	}
}

openApiConsole = function(){
	openAlertBox(''
		+'<form id="do_vk_api">'
				+'<div>Method:</div>'
				+'<input type="text" name="apiconsolemethod">'
				+'<div>Parameters (in JSON):</div>'
				+'<input type="text" name="apiconsoleparams">'
			+'</form>'
			+'<button onclick="prepApiCall();return false;">Do api request</button>',
		'closeAlertBox();',0);
	prepApiCall = function(){
		apimethod = $name('apiconsolemethod')[0].value;
		apiparams = JSON.parse($name('apiconsoleparams')[0].value);
		doApi(apimethod,apiparams);
	}
}

doApi = function(method,params){
	apicall(method,params,function(data){
		openAlertBox(''
				+'<div>Console Log:</div><textarea style="height:250px;width:700px;" type="text" name="vdescription">'+JSON.stringify(data)+'</textarea>',
			'closeAlertBox();',0);
	});
}

}